﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Pigs.Enemies;
using Pigs.Weapons;

namespace pigs
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Survival : Level
    {
        int pigsToAdd;
        const int delay = 5;
        int delayCounter;

        public Survival(GameSettings set)
            :base(set)
        {
            levelName = "sur";
            TransitionOnTime = TimeSpan.FromSeconds(1.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);

            pigsToAdd = 0;
            delayCounter = 0;
        }

        /// <summary>
        /// LoadContent images for gameplay
        /// </summary>
        public override void LoadContent()
        {
            wave = new Boolean[100];
            CurWave = 1;
            this.loadCommonContent();

            //load cloud
            Texture2D clouds = content.Load<Texture2D>("levels/demo/clouds");
            Scenery cloud = new Scenery(clouds, new Vector2(0, 0));
            cloud.initialize(-0.1f, false);
            background.Add(cloud);

            clouds = content.Load<Texture2D>("levels/demo/cloudsflipped");
            cloud = new Scenery(clouds, new Vector2(cloud.Width - 1, 0));
            cloud.initialize(-0.1f, false);
            background.Add(cloud);
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            this.commonUpdate(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            if (IsActive)
            {
                //at each level of 10 seconds send a new wave with one more pig
                //than the previous wave
                if (CurWave < 100 && levelTime > (10000 * CurWave - 1) && !wave[CurWave])
                {
                    pigsToAdd += (int)(Math.Pow((Math.Log(CurWave) + 1),2));
                    wave[CurWave] = true;
                    CurWave++;
                }
                
                if (delayCounter == 0)
                {
                    if (pigsToAdd > 0)
                    {
                        addNewPig();
                        pigsToAdd--;
                        delayCounter = delay;
                    }
                }
                else
                {
                    delayCounter--;
                }

                //if beat all 99 levels and no pigs left you winner
                if (CurWave == 100 && pigs.Count == 0)
                {
                    doEndGame(true);
                }

                //tile the background clouds
                if (((int)background.Last().rectangle.Right) < (viewport.Width + 100))
                {
                    //load cloud
                    Texture2D clouds = content.Load<Texture2D>("levels/demo/clouds");
                    Scenery cloud = new Scenery(clouds, new Vector2((int)background.Last().rectangle.Right - 1, 0));
                    cloud.initialize(-0.1f, false);
                    background.Add(cloud);

                    clouds = content.Load<Texture2D>("levels/demo/cloudsflipped");
                    cloud = new Scenery(clouds, new Vector2((int)background.Last().rectangle.Right - 1, 0));
                    cloud.initialize(-0.1f, false);
                    background.Add(cloud);
                }
            }
        }

        /// <summary>
        /// add a new pig to the game based on a random number
        /// </summary>
        public void addNewPig()
        {
            int r = rand.Next(0, 200);
            if (r < 76) addBasic(rand.Next(100,400), rand.Next(0, viewport.Height));
            else if (r < 96) addSuicide(rand.Next(100,400), rand.Next(0,viewport.Height));
            else if (r < 116) addDrunk(rand.Next(100, 400), rand.Next(0, viewport.Height));
            else if (r < 136) addMagic(rand.Next(100, 400), rand.Next(0, viewport.Height));
            else if (r < 156) addBunny(rand.Next(100, 400), rand.Next(0, viewport.Height));
            else if (r < 191) addNinja(0, rand.Next(50, viewport.Height));
            else if (r < 196) addLeader(0, rand.Next(50, 600));
            else addSteel(rand.Next(100, 600), getLowestPlayer().position.Y);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Draw(GameTime gameTime)
        {
            this.commonDraw(gameTime);

            spriteBatch.DrawString(scoreFont, "Wave: ", new Vector2(690, 4), Color.White);
            spriteBatch.DrawString(scoreFont, CurWave.ToString(), new Vector2(750, 4), Color.LawnGreen);


            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
